package org.dftproject.genesis.ui;

import org.dftproject.genesis.ui.actions.HomeAction;
import org.dftproject.genesis.ui.actions.NewTabAction;
import org.dftproject.genesis.ui.actions.PageHistoryAction;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private NewTabAction newTabAction;
	private IWorkbenchAction newWindowAction;
	private IWorkbenchAction exitAction;
	private IWorkbenchAction aboutAction;

	private IWorkbenchAction backAction;
	private IWorkbenchAction forwardAction;
	private HomeAction homeAction;
	
	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	@Override
	protected void makeActions(IWorkbenchWindow window) {
		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		aboutAction.setText("&About...");
		register(aboutAction);

		newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
		newWindowAction.setText("&New Window");
		register(newWindowAction);
		
		backAction = new PageHistoryAction(window, false);
		backAction.setId("backHistory");
        register(backAction);
        
        forwardAction = new PageHistoryAction(window, true);
        forwardAction.setId("forwardHistory");
        register(forwardAction);

		homeAction = new HomeAction(window, "Go to Start page");
		register(homeAction);
		
		newTabAction = new NewTabAction(window);
		register(newTabAction);
	}

	@Override
	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager("&Genesis", IWorkbenchActionConstants.M_FILE);
		MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP);

		menuBar.add(fileMenu);
		menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menuBar.add(helpMenu);

		fileMenu.add(newTabAction);
		fileMenu.add(newWindowAction);
		fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);

		helpMenu.add(aboutAction);
	}

	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {		
		IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
		toolbar.add(backAction);
		toolbar.add(forwardAction);
		toolbar.add(homeAction);
		toolbar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		
		coolBar.add(new ToolBarContributionItem(toolbar, "main"));
	}

}
